/**
 * 
 */
package com.cloudbroker.bcs.platform.ltb.common.payUtil;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
//import com.yunpay.demo.pay.bean.OrderResponse;

/**
 * @Class XMLUtil
 * @Description 用xstream 将bean转为xml的string
 * @author jiangchuan@hundsun.com
 * @date 2014年12月11日
 */
public class XMLUtil {
	private final static String HEAD = "<CFEP>";
	private final static String SIGN = "<Sign>";
	private final static String FOOT = "</Sign></CFEP>";
	private final static String REQUEST = "Request";
	private final static String RESPONSE = "Response";
	private final static String PUB = "Pub";

	/**
	 * @Method toXML
	 * @Description 将obj转化为xml格式的string
	 * @param Object obj 需要转化的实例
	 * @param String alias 类名替换
	 * @return String
	 * @throws
	 */
	public static String toXML(Object obj, String alias) {
		XStream xs = new XStream(new DomDriver());
		if (null != alias && !alias.isEmpty())
			xs.alias(alias, obj.getClass());

		String rtnValue = xs.toXML(obj);
		return rtnValue;
	}

	public static Object fromXML(String source) {
		XStream xs = new XStream(new DomDriver());
		Object obj = xs.fromXML(source);
		return obj;
	}

	public static String wrapMessage(Object body, Object head, String sign) {
		String mBody = toXML(body, REQUEST);
		String mHead = toXML(head, PUB);

		return (HEAD + mHead + mBody + SIGN + sign + FOOT).replaceAll("__", "_");
	}
	
//	public static String wrapMessageResp(OrderResponse body, Object head, String sign) {
//		String mBody = toXML(body, RESPONSE);
//		String mHead = toXML(head, PUB);
//
//		return (HEAD + mHead + mBody + SIGN + sign + FOOT).replaceAll("__", "_");
//	}
}
